package org.winexpress

import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;
import grails.converters.JSON

class CustomerController {
    
    // REST operations
    // GET
    def show = {
        println 'CustomerController >> Show >> START'
        
        CustomerManagement cman = new CustomerManagement()
        
        if (params.id && Customer.exists(params.id)) {
            def c = cman.getCustomerByID(params.id)
            if (c.activationStatus == true) {
                render c as JSON
            }
            else {
                JSONObject o = new JSONObject()
                o.put("404",true)
                render o as JSON
            }
            
            println 'Show customer by ID >> ' + c.id + " " + c
        }
//        else
//        if (params.username) {
//            def c = cman.getCustomerByUsername(params.username)
//            if (c.activationStatus == true) {
//                render c as JSON
//            }
//            else {
//                JSONObject o = new JSONObject()
//                o.put("404",true)
//                render o as JSON
//            }
//            
//            println 'Show customer by USERNAME >> ' + c.id + " " + c
//        }
        else {
            def allC = cman.listCustomers()
            if (allC.length() > 0) {
                render allC as JSON
            }
            else {
                JSONObject o = new JSONObject()
                o.put("404",true)
                render o as JSON
            }
            
            println 'Listing all customers >> ' + allC
        }
        
        println 'CustomerController >> Show >> END'
    }
    
    // PUT
    def update = {
        
        
//        if (params.id && Customer.exists(params.id) && params.postalCode && params.password) {
//            def c = cman.getCustomerByID(params.id)
       
        
        println 'CustomerController >> update >> START'
        
        JSONObject o = request.JSON
//        
        CustomerManagement cman = new CustomerManagement()
        
        def c = cman.getCustomerByID(o.get("id"))
        if (o.has("password")) { if (o.get("password") != "") c.password = o.get("password"); println c.password }
        if (o.has("activationStatus")) { c.activationStatus = o.get("activationStatus"); println c.activationStatus }
        if (o.has("blkHouseNo")) { c.blkHouseNo = o.get("blkHouseNo"); println c.blkHouseNo }
        if (o.has("streetName")) { c.streetName = o.get("streetName"); println c.streetName }
        if (o.has("postalCode")) { c.postalCode = o.get("postalCode"); println c.postalCode }
        if (o.has("telNo")) { c.telNo = o.get("telNo"); println c.telNo  }
        if (o.has("country")) { 
            CountryManagement ctyman = new CountryManagement()
            def cty = ctyman.getCountryByCountryName(o.get("country"))
            c.countryId = cty.id
        }  
        
        def result = cman.updateCustomer(c)
        if (result == true) {
            println 'CustomerController >> Update >> ' + result
//            render response as JSON
        }
        else {
            println 'CustomerController >> Update >> ' + result
        }
        
        println 'CustomerController >> update >> END'
       
    }
    
    // DELETE
    def delete = {
        println 'CustomerController >> delete >> START'
        
        println 'CustomerController >> delete >> END'
    }
    
    // POST
    def save = {
        println 'CustomerController >> save >> START'
        
        JSONObject o = request.JSON
        
        CustomerManagement cman = new CustomerManagement()
        def result = cman.addCustomer(o)
        if (result == true) {
            println 'CustomerController >> Update >> ' + result
//            render response as JSON
        }
        else {
            println 'CustomerController >> Update >> ' + result
        }
//        if (c.save(flush:true)) {
//            def response = true
//            render response as JSON
//
//            println 'Create Customer and Account >> ' + response
//        }
//        else {
//            def response = false
//            render response as JSON
//            
//            println 'Create Customer and Account >> ' + response
//        }

        println 'CustomerController >> save >> END'
    }
    
    // other supporting
//    def registerMember = {
//        def customer = request.JSON
//    }
    
//    def addMember = {
//        println request.JSON
//        
//        JSONObject o = request.JSON
//        Customer c = new Customer()
//        c.userName = o.get("userName")
//        c.password = o.get("password")
//        c.lastName = o.get("lastName")
//        c.firstName = o.get("firstName")
//        Date bd = new Date();
//        bd.parse("yyyy-MM-dd", o.get("birthdate")) 
//        c.birthdate = bd
//        c.blkHouseNo = o.get("blkHouseNo")
//        c.streetName = o.get("streetName")
//        c.postalCode = o.get("postalCode")
//        
//        def cty = Country.findByCountryName(o.get("country"))
//        println cty
//        c.country = cty
//        
//        c.telNo = o.get("telNo")
//        
//        // my null for lastlogin does not work!
//        Date d = new Date();
//        c.lastLogin = d
//        c.activationStatus = false
//        
//        c.save(flush:true)
//    }
    
//    def getCustomers = {
//        def c =  Customer.findAll()
//        render c as JSON
//        println c
//    }
    
}
